package com.jsz.peini.server.thread;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import com.jsz.peini.bean.order.GoldHistory;
import com.jsz.peini.bean.order.OrderInfo;
import com.jsz.peini.bean.redDate.RedDate;
import com.jsz.peini.bean.user.UserAccount;
import com.jsz.peini.bean.user.UserInfo;
import com.jsz.peini.mapper.order.GoldHistoryMapper;
import com.jsz.peini.mapper.order.OrderInfoMapper;
import com.jsz.peini.mapper.redDate.RedDateMapper;
import com.jsz.peini.mapper.user.UserAccountMapper;
import com.jsz.peini.mapper.user.UserInfoMapper;
import com.jsz.peini.redis.util.RedDateRedisUtil;
import com.jsz.peini.service.order.OrderInfoService;

public class RedDateThread extends Thread  {
     @Resource
     private RedDateMapper redDateMapper;
     @Resource
     private UserAccountMapper userAccountMapper;
     @Resource
     private UserInfoMapper userInfoMapper;
     @Resource
     private OrderInfoMapper orderInfoMapper;
     @Resource
     private GoldHistoryMapper goldHistoryMapper;
     @Resource
     private OrderInfoService orderInfoService;
	
	public void init(){
		this.start();
	}
	public void run() {
		super.run();
		while(true)
		{
			List<RedDate> list=RedDateRedisUtil.getRedDateList();
			if(list!=null)
			{
				if(list.size()>0)
				{
					for(RedDate redDate:list)
					{ 
						  if(redDate.getType()==0)
						  {
							    redDateMapper.updateByPrimaryKey(redDate);
							    redDate.setType(1);
							    RedDateRedisUtil.addRedDateRedis(redDate);
				        	    Integer goldsum=redDate.getVanum()*100;
			  	            	OrderInfo orderInfo = new OrderInfo();
			      	  			orderInfo.setUserId(redDate.getUserid());
			      	  			orderInfo.setOrderType(9);
			      	  			orderInfo.setOrderStatus(0);
			      	  			orderInfo.setAllMoney(0);
			      	  			orderInfo.setPayMoney(0);
			      	  			orderInfo.setDiscount(goldsum.toString());
			      	  			UserInfo userInfo = userInfoMapper.selectByPrimaryKey(redDate.getUserid());
			      	  			orderInfo.setUserPhone(userInfo.getUserName());
			      	  			orderInfo.setOrderTime(new Date());
			      	  			orderInfo.setCreateTime(new Date());
			      	  			orderInfoMapper.insert(orderInfo);
			      	  			Long orderId = orderInfo.getId();
			          	  		String code = "";
								code += orderInfo.getOrderType();
								SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
								code += sdf.format(new Date());
								String codeStr = String.valueOf(orderInfo.getId());
								if(codeStr.length()> 6){
									codeStr = codeStr.substring(codeStr.length()-6);
								}else if(codeStr.length() < 6){
									for (int ii = 0; ii < 6-codeStr.length(); ii++) {
										code += "0";
									}
								}
								code += codeStr;
			      	  			
			      	  			orderInfo.setOrderCode(code);
			      	  			orderInfoMapper.updateByPrimaryKeySelective(orderInfo);
			      	  		    orderInfoService.notifyPayOrder(orderInfo);
						  }
						    
		  	             
					}
				}
			}
		}


		
	}
}
